import puppeteer from 'puppeteer';

(async () => {
// Launch the browser and open a new blank page
const browser = await puppeteer.launch();
const page = await browser.newPage();

// Navigate the page to a URL
await page.goto('https://mypartners.xmcnbroker.info/#/login');

const title = await page.title();
console.log(`The title is: ${title}`);

// Set screen size
// await page.setViewport({width: 1080, height: 1024});

// Type into search box
// await page.type('.devsite-search-field', 'automate beyond recorder');

// Wait and click on first result
// const searchResultSelector = '.devsite-result-item-link';
// await page.waitForSelector(searchResultSelector);
// await page.click(searchResultSelector);

// Locate the full title with a unique string
// const textSelector = await page.waitForSelector(
//     'text/Customize and automate'
// );
// const fullTitle = await textSelector?.evaluate(el => el.textContent);

// Print the full title
// console.log('The title of this blog post is "%s".', fullTitle);

await browser.close();




const fs = require('fs')
const path = require('path')
const puppeteer = require('puppeteer')
const PDFDocument = require('pdfkit')
 
// 有时候操作需要暂停下
const sleep = (time) =>
  new Promise((resolve) => {
    setTimeout(resolve, time)
  })
 
const login = async ({ username, pwd }) => {
  // 使用puppeteer的launch（）方法获得一个操控浏览器的实例browser
  const browser = await puppeteer.launch({
    slowMo: 100, //放慢速度
    headless: false, //无头浏览器：关闭
    defaultViewport: { width: 1920, height: 1000 }, //设置网页的尺寸
    ignoreHTTPSErrors: false //忽略 https 报错
  })
  
  // 使用browser实例在内存中新建一个网页page
  const page = await browser.newPage()
  
  // 使用网页page的goto（）方法进入指定的花瓣网页面
  await page.goto('https://huaban.com/')
 
  // 先使用page.screenshot（）方法截图看下非登录页面
  await page.screenshot({
    path: path.resolve(__dirname, 'notLogin.png')
  })
 
  // 通过page.$()方法找到右上角登录按钮，点击
  const btnLogin = await page.$('.lUDCpxvZ')
  await btnLogin.click()
 
  // 找到登录弹框的其他登录方式，点击
  const btnOtherWay = await page.$('.__9_KlPtcX')
  await btnOtherWay.click()
 
  // 延迟1秒再操作
  sleep(1000)
 
  // 获取账号的DOM，对其进行操作，输入账号
  const inputUsername = await page.$('#email')
  inputUsername.focus()
  for (let i = 0; i < username.length; i++) {
    await inputUsername.type(username[i])
  }
 
  // 获取密码的DOM，对其进行操作，输入密码
  const inputPwd = await page.$('#password')
  inputPwd.focus()
  for (let i = 0; i < pwd.length; i++) {
    await inputPwd.type(pwd[i])
  }
 
  // 获取提交按钮的DOM
  const btnConfirm = await page.$('.CtUqjjIt')
  
  // 等待页面跳转完成，一般点击某个按钮需要跳转时，都需要等待 page.waitForNavigation() 执行完毕才表示跳转成功
  await Promise.all([btnConfirm.click(), page.waitForNavigation()])
 
  await sleep(3000)
 
  // 找到弹层关闭按钮，点击
  const btnClose = await page.$('.ant-modal-close')
  await btnClose.click()
}
 
login({ username: '13655246078', pwd: 'Zhu138157' })




const puppeteer = require('puppeteer');
const fs = require('fs');

// 定义cookie文件路径
const cookiePath = './cookies.json';

async function login() {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

  // 网站登录页的URL
  await page.goto('https://example.com/login');

  // 输入用户名和密码并登录
  await page.type('#username', 'your_username');
  await page.type('#password', 'your_password');
  await page.click('#login-button');

  // 等待页面加载完成
  await page.waitForNavigation();

  // 获取登录Cookie并保存到本地文件中
  const cookies = await page.cookies();
  fs.writeFileSync(cookiePath, JSON.stringify(cookies), 'utf-8');

  await browser.close();
}

async function main() {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

  // 在新的页面中加载网站主页
  await page.goto('https://example.com');

  // 检查本地是否有已保存的登录Cookie
  try {
    const cookiesString = fs.readFileSync(cookiePath, 'utf-8');
    const cookies = JSON.parse(cookiesString);

    // 设置页面的Cookie
    await page.setCookie(...cookies);
  } catch (err) {
    console.log('未找到已保存的Cookie');
  }

  // 访问需要登录权限的页面
  await page.goto('https://example.com/dashboard');

  await browser.close();
}

// 先执行登录任务，然后再执行主任务
login().then(() => main());





(async () => {
    const browser = await puppeteer.launch({
        slowMo: 100,    //放慢速度
        headless: false,
        defaultViewport: {width: 1440, height: 780},
        ignoreHTTPSErrors: false, //忽略 https 报错
        args: ['--start-fullscreen'] //全屏打开页面
    });
    const page = await browser.newPage();
    await page.goto('https://demo.youdata.com');
    //输入账号密码
    const uniqueIdElement = await page.$('#uniqueId');
    await uniqueIdElement.type('admin@admin.com', {delay: 20});
    const passwordElement = await page.$('#password', {delay: 20});
    await passwordElement.type('123456');
    //点击确定按钮进行登录
    let okButtonElement = await page.$('#btn-ok');
    //等待页面跳转完成，一般点击某个按钮需要跳转时，都需要等待 page.waitForNavigation() 执行完毕才表示跳转成功
    await Promise.all([
        okButtonElement.click(),
        page.waitForNavigation()  
    ]);
    console.log('admin 登录成功');
    await page.close();
    await browser.close();
})();



})();